package com.jatec.webcopy;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class WebItemManager extends BaseAdapter implements ListAdapter{

	private static WebItemManager instance = null;
	
    private ArrayList<WebItem> data = new ArrayList<WebItem>();
	
	private WebItemManager(){
		loadDummy();
	}
	
	public static WebItemManager instance(){
		if( instance == null ){
			instance = new WebItemManager();
		}
		
		return instance;
	}
	
	private void loadDummy(){
		
		WebItem item = new WebItem();
		item.setId(1);
		item.setName("vogella");
		item.setUrl("http://www.vogella.de/articles/AndroidListView/article.html");
		item.setLinkDepth(1);
		item.setLocalPath("/mnt/SDCARD");
		data.add(item);
		
		item = new WebItem();
		item.setId(2);
		item.setName("yahoo");
		item.setUrl("http://www.yahoo.com");
		item.setLinkDepth(1);
		item.setLocalPath("/mnt/SDCARD");
		data.add(item);
		
	}
	

	public void add(WebItem item){
		item.setId(data.size()+1);
		data.add(item);
		notifyDataSetChanged();
	}
	
	public void remove(WebItem item){
		data.remove(item);
		notifyDataSetChanged();
	}
	
	public WebItem[] readAll(){
		WebItem[] array = new WebItem[data.size()];		
		return data.toArray(array);
	}

	public int getCount() {
		return data.size();
	}

	public WebItem get(int position) {
		return data.get(position);
	}
	
	public Object getItem(int position) {
		return get(position);
	}

	public long getItemId(int position) {
		WebItem item = (WebItem)getItem(position);
		return item.getId();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = null;
		
		if( convertView == null ){
			LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.jatec_list_item, parent, false);
		}
		else{
			rowView = convertView;
		}
		
		TextView name = (TextView) rowView.findViewById(R.id.row_name);
		TextView url = (TextView) rowView.findViewById(R.id.row_url);
		TextView depth = (TextView) rowView.findViewById(R.id.row_depth);
		TextView path = (TextView) rowView.findViewById(R.id.row_localPath);
		
		WebItem item = (WebItem)getItem(position);
		
		name.setText(item.getName());
		url.setText(item.getUrl());
		
		return rowView;
	}

	
}
